﻿<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('DbBackup:page')">
        <a-form layout="inline">
          <a-row :gutter="30"
            ><a-col :md="6" :sm="24">
              <a-form-item label="备份时间">
                <a-range-picker
                  style="width: 100%"
                  placeholder="请选择备份时间"
                  @change="onChangeexecuteTime"
                />
              </a-form-item> </a-col
            ><a-col :md="6" :sm="24">
              <a-form-item label="应用系统">
                <a-input v-model="queryParam.applicationName" allow-clear placeholder=""/>
              </a-form-item> </a-col
            ><a-col :md="6" :sm="24">
              <a-form-item label="备份类型">
                <a-select
                  :allowClear="true"
                  style="width: 100%"
                  v-model="queryParam.backupType"
                  placeholder="请选择备份类型"
                >
                  <a-select-option v-for="(item, index) in backupTypeData" :key="index" :value="item.code">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-item> </a-col
            ><a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :scroll="{ x: 1500, y: `calc(100vh - ${tableTop + 222 + 'px'})` }"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <!-- <template class="table-operator" slot="operator" v-if="hasPerm('DbBackup:add')" >
          <a-button type="primary" v-if="hasPerm('DbBackup:add')" icon="plus" @click="$refs.addForm.add()">新增数据备份</a-button>
        </template> -->
        <span slot="backupTypescopedSlots" slot-scope="text">
          {{ 'BackupType' | enumType(text) }}
        </span>
        <span slot="testStatusscopedSlots" slot-scope="text">
          {{ 'TestStatus' | enumType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a-popconfirm
            v-if="hasPerm('DbBackup:edit')"
            placement="topRight"
            title="确认还原？"
            @confirm="() => DbBackupRestore(record)"
          >
          <a>还原</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('DbBackup:edit') & hasPerm('DbBackup:delete')" />
          <a-popconfirm
            v-if="hasPerm('DbBackup:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => DbBackupDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import moment from 'moment'
import { DbBackupPage, DbBackupDelete } from '@/api/modular/main/DbBackupManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
export default {
  components: {
    STable,
    addForm,
    editForm,
  },
  data() {
    return {
      tableTop: 0,
      queryParam: {},
      executeTimeDateString:'',
      endTimeDateString:'',
      columns: [
        {
          title: '备份文件',
          align: 'center',
          width: 150,
          ellipsis: true,
          dataIndex: 'fileName',
        },
        {
          title: '应用系统',
          align: 'center',
          width: 150,
          ellipsis: true,
          dataIndex: 'db.applicationName',
        },
        {
          title: '备份时间',
          align: 'center',
          width: 150,
          dataIndex: 'executeTime',
          customRender: (val) => {
            return val ? moment(val).format('YYYY-MM-DD HH:mm:ss') : ''
          },
        },
        {
          title: '备份类型',
          align: 'center',
          width: 100,
          dataIndex: 'backupType',
          scopedSlots: { customRender: 'backupTypescopedSlots' },
        },
        {
          title: '压缩备份',
          align: 'center',
          width: 80,
          customRender: (vlaue) => (vlaue ? <a-tag color="green">是</a-tag> : <a-tag color="volcano">否</a-tag>),
          dataIndex: 'isCompress',
        },
        {
          title: '备份错误消息',
          align: 'center',
          width: 150,
          ellipsis: true,
          dataIndex: 'backupErrorMessage',
        },
        {
          title: '远程存储错误消息',
          align: 'center',
          ellipsis: true,
          width: 150,
          dataIndex: 'remoteStoreErrorMessage',
        },
        {
          title: '还原测试状态',
          align: 'center',
          width: 100,
          dataIndex: 'testStatus',
          scopedSlots: { customRender: 'testStatusscopedSlots' },
        },
        {
          title: '最后测试时间',
          align: 'center',
          width: 150,
          dataIndex: 'testTime',
          customRender: (val) => {
            return val ? moment(val).format('YYYY-MM-DD HH:mm:ss') : ''
          },
        },
        {
          title: '备份测试错误消息',
          align: 'center',
          ellipsis: true,
          width: 150,
          dataIndex: 'testErrorMessage',
        },
        {
          title: '测试数据库服务器',
          align: 'center',
          width: 150,
          dataIndex: 'testServerId',
        },
        {
          title: '存放文件夹路径',
          align: 'center',
          width: 150,
          ellipsis: true,
          dataIndex: 'storePathLocal',
        },
        {
          title: '远程存放路径',
          align: 'center',
          width: 150,
          ellipsis: true,
          dataIndex: 'storePathRemote',
        },
        {
          title: '删除时间',
          align: 'center',
          width: 150,
          dataIndex: 'deleteTime',
          customRender: (val) => {
            return val ? moment(val).format('YYYY-MM-DD HH:mm:ss') : ''
          },
        },
        {
          title: '本地已删除',
          align: 'center',
          width: 100,
          customRender: (vlaue) => (vlaue ? <a-tag color="green">是</a-tag> : <a-tag color="volcano">否</a-tag>),
          dataIndex: 'isLocalBackupDeleted',
        },
        {
          title: '远程已删除',
          align: 'center',
          width: 100,
          customRender: (vlaue) => (vlaue ? <a-tag color="green">是</a-tag> : <a-tag color="volcano">否</a-tag>),
          dataIndex: 'isRemoteBackupDeleted',
        },
        {
          title: '数据库服务名称',
          align: 'center',
          width: 120,
          dataIndex: 'dbServer.dbName',
        },
        {
          title: '数据库名称',
          align: 'center',
          width: 100,
          dataIndex: 'db.DbName',
        },
        {
          title: '月末备份',
          align: 'center',
          width: 80,
          customRender: (vlaue) => (vlaue ? <a-tag color="green">是</a-tag> : <a-tag color="volcano">否</a-tag>),
          dataIndex: 'isMonthly',
        },
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return DbBackupPage(Object.assign(parameter, this.switchingDate())).then((res) => {
          return res.data
        })
      },
      backupTypeData: [],
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {
    if (this.hasPerm('DbBackup:edit') || this.hasPerm('DbBackup:delete')) {
      this.columns.push({
        title: '操作',
        width: '130px',
        align: 'center',
        fixed: 'right',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' },
      })
    }
    const backupTypeOption = this.$options
    this.backupTypeData = backupTypeOption.filters['enumData']('BackupType')
  },
  methods: {
    moment,
    /**
     * 查询参数组装
     */
    switchingDate() {
      if (this.executeTimeDateString != null && this.executeTimeDateString!=='') {
        this.queryParam.executeTime = moment(this.executeTimeDateString).format()
      } else {
        delete this.queryParam.executeTime
      }
      
      if (this.endTimeDateString != null && this.endTimeDateString!=='') {
        this.queryParam.endTimeDate = moment(this.endTimeDateString).format()
      } else {
        delete this.queryParam.endTimeDate
      }


      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    DbBackupDelete(record) {
      DbBackupDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    DbBackupRestore(record) {
      this.$message.success('已启动还原，请耐心等待！')
    },
    onChangeexecuteTime(date, dateString) {
      this.executeTimeDateString = dateString[0]
      this.endTimeDateString = dateString[1]
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
